/**
 * Author@ Cheng Feitian
 * Date: 2020-3-19
 * Description: 
 */

#include <algorithm>
#include <iostream>
#include <string>

#define SUCCESS 0;
#define FAILURE -1;

using namespace std;
const int maxn = 1024;

typedef int Status;

//联系人结构体
typedef struct Person
{
	string m_Name; //姓名
	int m_Gender;  //性别：1.男 2.女
	int m_Age;	   //年龄
	string m_Phone;
	string m_Addr;
} Person;

//通讯录结构体
typedef struct AddressBooks
{
	//通讯录中保存的联系人数组
	Person personArray[maxn];
	//通讯录中当前记录联系人个数
	int m_Size;
} AddressBooks;

bool addPerson(AddressBooks *abs);

void showMenu()
{
	cout << "***************************" << endl;
	cout << "*****  1、添加联系人  *****" << endl;
	cout << "*****  2、显示联系人  *****" << endl;
	cout << "*****  3、删除联系人  *****" << endl;
	cout << "*****  4、查找联系人  *****" << endl;
	cout << "*****  5、修改联系人  *****" << endl;
	cout << "*****  6、清空联系人  *****" << endl;
	cout << "*****  0、退出通讯录  *****" << endl;
	cout << "***************************" << endl;
}
bool addPerson(AddressBooks *abs)
{
	bool status;
	if (abs->m_Size == maxn)
	{
		cout << "通讯录已满，无法添加！" << endl;
		status = false;
	}
	else
	{
		//添加具体联系人

		/**
		 * @brief properties:
		 * Name:
		 * Gender:
		 * Age:
		 * Phone:
		 * Address:
		 */

		string name;
		cout << "请输入姓名：" << endl;
		cin >> name;
		abs->personArray[abs->m_Size].m_Name = name;

		int gender = 0;
		while (true)
		{
			cout << "请输入性别：" << endl;
			cout << "1 - man\t"
				 << "2- woman" << endl;
			cin >> gender;
			if (gender == 1 || gender == 2)
			{
				abs->personArray[abs->m_Size].m_Gender = gender;
				break;
			}
			cout << "Wrong Input,please try again!" << endl;
		}

		cout << "请输入年龄：" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->m_Size].m_Age = age;

		cout << "请输入电话：" << endl;
		string phone;
		cin >> phone;
		abs->personArray[abs->m_Size].m_Phone = phone;

		cout << "请输入家庭住址：" << endl;
		string address;
		cin >> address;
		abs->personArray[abs->m_Size].m_Addr = address;

		abs->m_Size++;

		status = true;
		system("cls"); //清屏操作
	}
	return status;
}

/* 显示所有联系人 */
void showPerson(const AddressBooks *abs)
{
	//判断通讯录中人数是否为0，如果为0，提示记录为空
	//如果不为0，显示记录的联系人信息

	if (abs->m_Size == 0)
	{
		cout << "There has no person!" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_Size; i++)
		{
			cout << "Name:" << abs->personArray[i].m_Name << "\t"
				 << "Gender:" << ((abs->personArray[i].m_Gender == 1) ? "男" : "女") << "\t"
				 << "Age:" << abs->personArray[i].m_Age << "\t"
				 << "Phone Number:" << abs->personArray[i].m_Phone << "\t"
				 << "Address:" << abs->personArray[i].m_Addr << endl;
		}
	}
}

Status isExist(const AddressBooks *abs, string name)
{
	int flag = FAILURE;
	for (int i = 0; i < abs->m_Size; i++)
	{
		if (abs->personArray[i].m_Name == name)
		{
			flag = i; //找到后返回该联系人在数组中的位置
			break;
		}
		else
		{
			flag = FAILURE; //否则返回不存在
		}
	}

	return flag;
}

void deletePerson(AddressBooks *abs)
{
	cout << "请输入您要删除的联系人" << endl;

	string name;
	cin >> name;

	int ret = isExist(abs, name);

	if (ret != -1)
	{
		for (int i = ret; i < abs->m_Size; i++)
		{
			abs->personArray[i] = abs->personArray[i + 1]; //注意这里是有问题的,i+1 在表满时可能会造成越界
		}
		abs->m_Size--;
		cout << "删除成功" << endl;
	}
	else
	{
		cout << "没有此人！" << endl;
	}
}

void findPerson(const AddressBooks *abs)
{

	cout << "请输入您要查找的联系人" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1)
	{
		cout << "Name:" << abs->personArray[ret].m_Name << "\t"
			 << "Gender:" << ((abs->personArray[ret].m_Gender == 1) ? "男" : "女") << "\t"
			 << "Age:" << abs->personArray[ret].m_Age << "\t"
			 << "Phone Number:" << abs->personArray[ret].m_Phone << "\t"
			 << "Address:" << abs->personArray[ret].m_Addr << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
}

void modifyPerson(AddressBooks *abs)
{
	bool flag = true;
	int wrongTime = 0;
	while (flag)
	{
		cout << "请输入您要修改的人的名称：";
		string name;
		cin >> name;

		int ret = isExist(abs, name);
		if (ret != -1)
		{
			string name;
			cout << "请输入姓名：" << endl;
			cin >> name;
			abs->personArray[ret].m_Name = name;

			int gender = 0;
			while (true)
			{
				cout << "请输入性别：" << endl;
				cout << "1 - man\t"
					 << "2- woman" << endl;
				cin >> gender;
				if (gender == 1 || gender == 2)
				{
					abs->personArray[ret].m_Gender = gender;
					break;
				}
				cout << "Wrong Input,please try again!" << endl;
			}

			cout << "请输入年龄：" << endl;
			int age = 0;
			cin >> age;
			abs->personArray[ret].m_Age = age;

			cout << "请输入电话：" << endl;
			string phone;
			cin >> phone;
			abs->personArray[ret].m_Phone = phone;

			cout << "请输入家庭住址：" << endl;
			string address;
			cin >> address;
			abs->personArray[ret].m_Addr = address;

			flag = false;
		}
		else
		{
			cout << "查无此人，请重新输入！" << endl;
			wrongTime++;
			flag = true;
			if (wrongTime >= 3)
			{
				flag = !flag;
			}
		}
	}
}

void cleanPerson(AddressBooks *abs)
{
	abs->m_Size = 0;
	cout << "通讯录已清空" << endl;
}

int main()
{

	//创建通讯录结构体变量
	AddressBooks abs;
	//初始化通讯录中当前人员个数
	abs.m_Size = 0;

	int select = 0;
	bool flag = true;

	while (flag)
	{
		showMenu();

		cin >> select;

		switch (select)
		{
		case 1: //添加联系人
			if (addPerson(&abs))
			{
				cout << "Update SUCCESS" << endl;
			}
			break;
		case 2: //显示联系人
			showPerson(&abs);
			break;
		case 3: //删除联系人
		{
			// cout << "请输入要删除的联系人的姓名：" << endl;
			// string name;
			// cin >> name;

			// if (isExist(&abs, name) == -1)
			// {
			// 	cout << "查无此人" << endl;
			// }
			// else
			// {
			// 	cout << "找到此人" << endl;
			// }
			deletePerson(&abs);
			break;
		}
		case 4: //查找联系人
			findPerson(&abs);
			break;
		case 5: //修改联系人
			modifyPerson(&abs);
			break;
		case 6: //清空联系人
			cleanPerson(&abs);
			break;
		case 0: //退出通讯录
			cout << "欢迎下次使用" << endl;
			flag = false;
			break;
		default:
			break;
		}
		system("pause");
		system("cls");
	}

	return 0;
}
